package com.dykj.fireFighting.web.app;

import com.dykj.fireFighting.api.base.devHisResult.entity.DevHisResult;
import com.dykj.fireFighting.api.base.devHisResult.service.IDevHisResultService;
import com.dykj.fireFighting.api.elasticsearch.devHis.entity.EsDevHis;
import com.dykj.fireFighting.api.elasticsearch.devHis.service.EsDevHisService;
import com.dykj.fireFighting.api.socialUnit.devHis.entity.DevHis;
import com.dykj.fireFighting.api.socialUnit.devHis.service.IDevHisService;
import com.dykj.fireFighting.api.socialUnit.devHis.vo.DevHisVo;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.Device;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author fgc
 * @Date 2021/7/13 10:57
 * @ Description：
 */
@Slf4j
@Api(value = "app设备管理接口", tags = {"app设备管理接口"})
@RestController
@RequestMapping("/app/devHis")
public class AppDevHisController {

    @DubboReference
    private IDevHisService devHisService;

    @DubboReference
    private IDevHisResultService devHisResultService;
    @DubboReference
    private EsDevHisService esDevHisService;
    @DubboReference
    private IDeviceService deviceService;

    /**
     * 获取登录用户的单位（监控中心）id
     */
    public Long getEnterpriseId() {
        try {
            Long enterpriseId = ShiroUtil.getUser().getEnterpriseId();
            return enterpriseId;
        }catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("请登录");
        }
    }


    /**
     * 设备管理-查看设备运行记录
     *
     * @return
     */
    @AutoLog(value = "设备管理-智能设备-查看设备运行记录")
    @ApiOperation(value = "设备管理-查看设备运行记录", notes = "设备管理-查看设备运行记录")
    @GetMapping("/queryByrelationId")
   /* @RequiresPermissions("device/list")*/
    public CommonResult<PageVo<DevHisVo>> queryByrelationId(@RequestParam long page, @RequestParam long limit, @RequestParam String relationId) {
        PageVo<DevHisVo> devHisVoPageVo = devHisService.queryDevHisByRelationId(relationId, page, limit);
        return CommonResult.success(devHisVoPageVo);
    }

    @AutoLog(value = "APP设备报警数据")
    @ApiOperation(value = "APP设备报警数据", notes = "APP设备报警数据")
    @GetMapping("/num")
    public CommonResult getNum() {
        DevHisResult unitNum = devHisResultService.getUnitNum(getEnterpriseId() + "", null, null);
        // 火警数量
        Integer fireAlarm = unitNum.getFireAlarm();
        // 复核总数
        Integer reviewAll = unitNum.getReviewAll();
        // 预警总数
        Integer earlyWarning = unitNum.getEarlyWarning();
        // 处置总数
        Integer managementAll = unitNum.getManagementAll();
        HashMap<String, Object> map = new HashMap<>(8);
        map.put("waitReview",fireAlarm - reviewAll);
        map.put("waitManagement",earlyWarning - managementAll);
        return CommonResult.success(map);
    }

    /**
     * 获取未读设备故障记录数量
     * @return
     */
    @AutoLog(value = "APP设备未读故障记录数量")
    @ApiOperation(value = "APP设备未读故障记录数量", notes = "APP设备未读故障记录数量")
    @RequestMapping("getFaultNotReadNum")
    public CommonResult getFaultNotReadNum(){
        Long enterpriseId = getEnterpriseId();
        PageVo<EsDevHis> pageVo = esDevHisService.searchGz(enterpriseId.toString(), "0", 1, 10);
        return CommonResult.success( pageVo.getTotalCount());
    }

    /**
     * APP设备故障记录
     * @param readType 0-未读，1-已读
     * @return
     */
    @AutoLog(value = "APP设备故障记录")
    @ApiOperation(value = "APP设备故障记录", notes = "APP设备故障记录")
    @RequestMapping("getFaultList")
    public CommonResult getFaultList(@RequestParam long page, @RequestParam long limit,@RequestParam String readType){
        Long enterpriseId = getEnterpriseId();
        PageVo<EsDevHis> pageVo = esDevHisService.searchGz(enterpriseId.toString(), readType, (int)page, (int)limit);
        for (int i = 0; i < pageVo.getList().size(); i++) {
            Device device=deviceService.getDeviceByRelationId(pageVo.getList().get(i).getRelationId());
            if(null!=device){
                pageVo.getList().get(i).setDevType(device.getTypeName());
            }
            EsDevHis esDevHis=new EsDevHis();
            esDevHis.setId(pageVo.getList().get(i).getId());
            esDevHis.setIsRead("1");
            esDevHisService.updateEsDevHisIsRead(esDevHis);

            DevHis devHis=new DevHis();
            devHis.setId(pageVo.getList().get(i).getId());
            devHis.setIsRead("1");
            devHisService.updateDevHis(devHis);

        }
        return CommonResult.success(pageVo);
    }
    /**
     * APP设备其他记录
     *
     * @return
     */
    @AutoLog(value = "APP设备其他记录")
    @ApiOperation(value = "APP设备其他记录", notes = "APP设备其他记录")
    @RequestMapping("getOtherList")
    public CommonResult getOtherList(@RequestParam long page, @RequestParam long limit){
        Long enterpriseId = getEnterpriseId();
        PageVo<EsDevHis> pageVo = esDevHisService.searchOther(enterpriseId.toString(), (int) page, (int) limit);
        for (int i = 0; i < pageVo.getList().size(); i++) {
            Device device=deviceService.getDeviceByRelationId(pageVo.getList().get(i).getRelationId());
            if(null!=device){
                pageVo.getList().get(i).setDevType(device.getTypeName());
            }
        }
        return CommonResult.success(pageVo);
    }
}
